package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    /**
     * 分页查询
     *
     * @param contractProductExample 分页查询的参数
     * @param pageNum                当前页
     * @param pageSize               页大小
     * @return
     */
    @Override
    public PageInfo<ContractProduct> findByPage(ContractProductExample contractProductExample, int pageNum, int pageSize) {
        Page<ContractProduct> page = PageHelper.startPage(pageNum, pageSize);
        List<ContractProduct> list = contractProductDao.selectByExample(contractProductExample);
        PageInfo<ContractProduct> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询所有
     *
     * @param contractProductExample
     */
    @Override
    public List<ContractProduct> findAll(ContractProductExample contractProductExample) {
        List<ContractProduct> list = contractProductDao.selectByExample(contractProductExample);
        return list;
    }

    /**
     * 根据id查询
     *
     * @param id
     */
    @Override
    public ContractProduct findById(String id) {
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        return contractProduct;
    }

    /**
     * 新增
     *
     * @param contractProduct
     */
    @Override
    public void save(ContractProduct contractProduct) {
        contractProduct.setId(UUID.randomUUID().toString());
        System.out.println("==============contractID==========" + contractProduct.getContractId());
        /**
         * 1.获得商品价格
         * 2.获取订单总价
         * 3.商品数量+1
         * 4.保存
         */
        Double amount = 0d;
        if (contractProduct.getPrice() != null && contractProduct.getCnumber() != null) {
            amount = contractProduct.getPrice() * contractProduct.getCnumber();
        }
        contractProduct.setAmount(amount);
        //获取订单
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //设置订单总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //设置商品数量
        contract.setProNum(contract.getProNum() + 1);
        //修改
        contractDao.updateByPrimaryKeySelective(contract);
        //保存货物
        contractProductDao.insertSelective(contractProduct);
    }

    /**
     * 修改
     *
     * @param contractProduct
     */
    @Override
    public void update(ContractProduct contractProduct) {
        Double newamount = 0d;
        //判空,计算新的商品总价
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null) {
            newamount = contractProduct.getPrice() * contractProduct.getCnumber();
        }

        contractProduct.setAmount(newamount);

        //获取商品之前的总价格
        ContractProduct cp = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        Double oldamount = cp.getAmount();
        //获取订单
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //获取原来的订单总价
        Double totalAmount = contract.getTotalAmount();
        //设置新的订单总价
        contract.setTotalAmount(totalAmount + newamount - oldamount);
        //更新订单
        contractDao.updateByPrimaryKeySelective(contract);
        //修改商品
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    /**
     * 删除商品
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //获取货物总金额
        Double amount = contractProduct.getAmount();
        //获取货物下的所有附件
        ExtCproductExample example = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = example.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(example);
        //便利附件累加附件总金额，并删除附件
        Double extAmount = 0d;
        if (extCproducts != null && extCproducts.size() > 0) {
            for (ExtCproduct extCproduct : extCproducts) {
                extAmount += extCproduct.getAmount();
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }
        //修改合同商品总价格
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        Double oldtotalAmount = contract.getTotalAmount();
        contract.setTotalAmount(oldtotalAmount - amount - extAmount);
        //修改合同商品数量
        contract.setProNum(contract.getProNum() - 1);
        contract.setExtNum(contract.getExtNum() - extCproducts.size());
        contractDao.updateByPrimaryKeySelective(contract);
        //删除商品
        contractProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<ContractProductVo> findByShipTime(String loginCompanyId, String inputDate) {
        return contractProductDao.findByShipTime(loginCompanyId,inputDate);
    }
}
